package com.lagou.client;

import com.lagou.model.*;

import java.io.IOException;

/**
 * 编程实现客户端的主界面绘制和相应功能的实现
 */
public class ClientView {
    /**
     * 为了可以使用输入输出流，采用合成复用原则实现
     */
    private ClientInitClose cic;
    private String UsrName;
    private String code;
    private String question;
    private String answer;
    private int score;

    /**
     * 通过构造方法实现成员变量的初始化
     * @param cic
     */
    public ClientView(ClientInitClose cic) {
        this.cic = cic;
    }

    /**
     * 自定义成员方法实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
         while(true) {
            System.out.println("  \n\n\t\t   在线考试系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员系统");
            System.out.println("     [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    StudentStart();
                    break;
                case 2:
                    clientManagerLogin();
                    break;
                case 0:
                    System.out.println("正在退出系统...");
                    cic.clientClose();
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 自定义成员方法实现客户端管理员登录的功能
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        // 1.准备管理员登录的相关数据
        System.out.println("请输入管理员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("managerCheck", new User(userName, password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送管理员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            ManagerStart();
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    /**
     * 自定义成员方法实现客户端学员登录的功能
     */
    private void clientStudentLogin() throws IOException, ClassNotFoundException {

        // 1.准备学员登录的相关数据
        System.out.println("请输入学员的账户信息：");
        String userName = ClientScanner.getScanner().next();
        UsrName = userName;
        System.out.println("请输入学员的密码信息：");
        String password = ClientScanner.getScanner().next();
        code = password;
        UserMessage tum = new UserMessage("userCheck", new User(UsrName, code));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送学员账户信息成功！");
        // 3.接收服务器的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())) {
            System.out.println("登录成功，欢迎使用！");
            StudentStart();
        } else {
            System.out.println("用户名或密码错误！");
        }
    }
    public void StudentStart() throws IOException, ClassNotFoundException {
        System.out.println("-------------------------------------------");
        System.out.println("欢迎进入学员系统");
        System.out.println("请选择要进行的业务编号：");
        System.out.print("   [1] 用户模块");
        System.out.println("     [2] 考试模块");
        int choose2 = ClientScanner.getScanner().nextInt();
        switch (choose2){
            case 1:
                userModule();
                break;
            case 2:
                testModule();
                break;
            default: break;
        }
    }

    private void userModule() throws IOException, ClassNotFoundException {
        System.out.println("-------------------------------------------");
        System.out.println("欢迎进入用户模块");
        System.out.println("请选择要进行的业务编号：");
        System.out.print("   [1] 登陆");
        System.out.println("     [2] 修改密码");
        System.out.println("     [3] 退出");
        int choose = ClientScanner.getScanner().nextInt();
        switch (choose){
            case 1:
                clientStudentLogin();
                break;
            case 2:
                changePassword();
                break;
            case 3:
                System.out.println("正在退出系统...");
                cic.clientClose();
                return;
            default: break;
        }
    }

    private void changePassword() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入新密码");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("ModifyPassword", new User( UsrName,password));
        cic.getOos().writeObject(tum);
        if ("PasswordModified".equals(tum.getType())) {
            System.out.println("密码修改成功成功");
            code = password;
        } else {
            System.out.println("该用户不存在");
        }
    }

    private void testModule() throws IOException, ClassNotFoundException {
        System.out.println("-------------------------------------------");
        System.out.println("欢迎进入考试模块");
        System.out.println("请选择要进行的业务编号：");
        System.out.print("   [1] 开始考试");
        System.out.println("     [2] 查询成绩");
        int choose2 = ClientScanner.getScanner().nextInt();
        switch (choose2){
            case 1:
                TestStart();
                break;
            case 2:
                getScore();
                break;
        }
    }

    private void TestStart() throws IOException, ClassNotFoundException {
        TestMessage ttm = new TestMessage("TestStart", new Test(UsrName, code));
        cic.getOos().writeObject(ttm);
        if ("TestAllowed".equals(ttm.getType())) {
            System.out.println("-------------------------------------------");
            System.out.println("考试开始");
            while (true){
                ttm = (TestMessage) cic.getOis().readObject();
                if(ttm.getType().equals("EndExam")){
                    System.out.println("考试结束！");
                    break;
                }
                System.out.println("接收到一道新题目：");
                System.out.println(ttm.getTest().getQuestion());//打印题目的描述
                question = ttm.getTest().getQuestion();
                System.out.println("请填写你的答案然后回车");
                String MyAnswer = ClientScanner.getScanner().next();
                answer = MyAnswer;
                ttm = new TestMessage("submit", new Test(question, answer));
                // 2.将UserMessage类型的对象通过对象输出流发送给服务器
                cic.getOos().writeObject(ttm);
                System.out.println("客户端成功发送用户写出的答案！");
            }
        } else {
            System.out.println("无法再次参加考试哦");
        }

    }

    private void getScore() throws IOException, ClassNotFoundException {
        TestMessage ttm = new TestMessage("getScore", new Test(UsrName, score));
        cic.getOos().writeObject(ttm);
        System.out.println("-------------------------------------------");
        System.out.println("客户端成功发送查询学生成绩的请求！");
        ttm = (TestMessage) cic.getOis().readObject();
        score = ttm.getTest().getGrade();
        if ("ScoreSent".equals(ttm.getType())) {
            System.out.println("你的成绩是：" + score + "分");
        } else {
            System.out.println("你还没有成绩");
        }
    }
    //管理员系统开始界面
    public void ManagerStart() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("欢迎进入管理员系统");
        System.out.println("请选择要进行的业务编号：");
        System.out.print("   [1] 学员管理模块");
        System.out.println("     [2] 考题管理模块" );
        int choose3 = ClientScanner.getScanner().nextInt();
        switch (choose3){
            case 1:
                System.out.println("学员管理模块");
                studentManage();
            case 2:
                System.out.println("考题管理模块");
                testManage();
            default: break;
        }
    }
    //学员管理
    private void studentManage() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("欢迎进入学员管理模块");
        System.out.println("请选择要进行的业务编号：");
        System.out.print("   [1] 增加学员");
        System.out.println("     [2] 删除学员" );
        System.out.println("     [3] 修改学员" );
        System.out.println("     [4] 查找学员" );
        int choose = ClientScanner.getScanner().nextInt();
        switch (choose){
            case 1: addStudent();studentManage();break;
            case 2: deleteStudent(); studentManage();break;
            case 3: modifyStudent();studentManage();break;
            case 4: searchStudent(); studentManage(); break;
            default: break;
        }
    }
    //增加学员成员方法
    private void addStudent() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入要增加的学员账号");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的学员密码");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("AddStudent", new User(username, password));
        cic.getOos().writeObject(tum);
        System.out.println("客户端已发送增加请求");
        System.out.println("-------------------------------------------");
        if ("StudentAdded".equals(tum.getType())) {
            System.out.println("新增用户成功");
        } else {
            System.out.println("该用户已存在");
        }
    }
    //删除学员成员方法
    private void deleteStudent() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入要删除的学员账号");
        String username = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("DeleteStudent", new User(username, ""));
        cic.getOos().writeObject(tum);
        System.out.println("客户端已发送删除请求");
        System.out.println("-------------------------------------------");
        if ("StudentDeleted".equals(tum.getType())) {
            System.out.println("删除用户成功");
        } else {
            System.out.println("该用户不存在");
        }
    }
    //修改学员成员方法
    private void modifyStudent() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入要修改的学员账号");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入要修改的学员密码");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("ModifyStudent", new User(username, password));
        cic.getOos().writeObject(tum);
        System.out.println("客户端已发送修改请求");
        System.out.println("-------------------------------------------");
        if ("StudentModified".equals(tum.getType())) {
            System.out.println("修改用户成功");
        } else {
            System.out.println("该用户不存在");
        }
    }
    //查找学员成员方法
    private void searchStudent() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入要查找的学员账号");
        String username = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("SearchStudent", new User(username, ""));
        cic.getOos().writeObject(tum);
        System.out.println("客户端已发送增加请求");
        System.out.println("-------------------------------------------");
        if ("StudentSearched".equals(tum.getType())) {
            System.out.println("该用户的学号为"+tum.getUser().getUserName()+"。 该用户的密码为"+tum.getUser().getPassword());
        } else {
            System.out.println("该用户不存在");
        }
    }
    //考试管理
    private void testManage() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("欢迎进入考试管理模块");
        System.out.println("请选择要进行的业务编号：");
        System.out.print("   [1] 增加考题");
        System.out.println("     [2] 删除考题" );
        System.out.println("     [3] 修改考题" );
        System.out.println("     [4] 查找考题" );
        int choose2 = ClientScanner.getScanner().nextInt();
        switch (choose2){
            case 1: addTest(); break;
            case 2: deleteTest(); break;
            case 3: modifyTest(); break;
            case 4: searchTest(); break;
            default: break;
        }
    }

    //增加考题成员方法
    private void addTest() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入要增加的考题信息");
        String question = ClientScanner.getScanner().next();
        System.out.println("请输入要增加的考题答案");
        String answer = ClientScanner.getScanner().next();
        TestMessage ttm = new TestMessage("AddTest", new Test(question,answer));
        cic.getOos().writeObject(ttm);
        System.out.println("客户端已发送增加请求");
        System.out.println("-------------------------------------------");
        if ("TestAdded".equals(ttm.getType())) {
            System.out.println("新增考题成功");
        } else {
            System.out.println("该考题已存在");
        }
    }

    //删除考题成员方法
    private void deleteTest() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入要删除的考题信息");
        String question = ClientScanner.getScanner().next();
        TestMessage ttm = new TestMessage("DeleteTest", new Test(question,""));
        cic.getOos().writeObject(ttm);
        System.out.println("客户端已发送删除请求");
        System.out.println("-------------------------------------------");
        if ("TestDeleted".equals(ttm.getType())) {
            System.out.println("删除考题成功");
        } else {
            System.out.println("该考题已存在");
        }
    }

    //修改考题成员方法
    private void modifyTest() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入要修改的考题信息");
        String question = ClientScanner.getScanner().next();
        System.out.println("请输入要修改的考题答案");
        String answer = ClientScanner.getScanner().next();
        TestMessage ttm = new TestMessage("ModifyTest", new Test(question,answer));
        cic.getOos().writeObject(ttm);
        System.out.println("客户端已发送修改请求");
        System.out.println("-------------------------------------------");
        if ("TestModified".equals(ttm.getType())) {
            System.out.println("修改考题成功");
        } else {
            System.out.println("该考题不存在");
        }
    }

    //查找考题成员方法
    private void searchTest() throws IOException {
        System.out.println("-------------------------------------------");
        System.out.println("请输入要查找的考题信息");
        String question = ClientScanner.getScanner().next();
        TestMessage ttm = new TestMessage("SearchTest", new Test(question,""));
        cic.getOos().writeObject(ttm);
        System.out.println("客户端已发送查找请求");
        System.out.println("-------------------------------------------");
        if ("TestSearched".equals(ttm.getType())) {
            System.out.println("已查找到考题"+ttm.getTest().getQuestion()+" 该题目答案为"+ttm.getTest().getAnswer());
        } else {
            System.out.println("该考题不存在");
        }
    }
}
